package org.bsplus.client.mvp;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.Observable;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.AnchorData;
import com.extjs.gxt.ui.client.widget.layout.AnchorLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.MarginData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;

public class MainView extends LayoutContainer implements MainPresenter.Display
{
    private LayoutContainer centreHolder = new LayoutContainer();
    private ContentPanel west = new ContentPanel();

    @Override
    protected void onRender(Element target, int index)
    {
        super.onRender(target, index);

        setStyleAttribute("background-color", "white");

        centreHolder.setLayout(new AnchorLayout());
        centreHolder.setScrollMode(Scroll.AUTOX);
        centreHolder.setLayoutOnChange(true);
        
        final BorderLayout layout = new BorderLayout();
        setLayout(layout);
        setStyleAttribute("padding", "10px");

        LayoutContainer north = new LayoutContainer(new AnchorLayout());
        north.setStyleName("header");
        
        west.setHeading("Menu");
        west.setFrame(true);
        
        BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 50);
        northData.setCollapsible(false);
        northData.setFloatable(false);
        northData.setHideCollapseTool(true);
        northData.setSplit(false);
        northData.setMargins(new Margins(0, 0, 5, 0));

        BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 150);
        westData.setSplit(true);
        westData.setCollapsible(true);
        westData.setMargins(new Margins(0, 5, 0, 0));

        BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
        centerData.setMargins(new Margins(0));

        add(north, northData);
        add(west, westData);
        add(centreHolder, centerData);
    }

    @Override
    public Widget asWidget()
    {
        return this;
    }

    @Override
    public void startProcessing()
    {
    }

    @Override
    public void stopProcessing()
    {
    }

    @Override
    public void setCentreComponent(Widget widget)
    {
        centreHolder.removeAll();
        centreHolder.add(widget, new AnchorData("100%"));
    }

    @Override
    public Observable addMenuItem(String name)
    {
        Button button = new Button();
        button.setText(name);
        button.setWidth("90%");
        west.add(button, new MarginData(5));
        return button;
    }
}
